---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: voip-manager
rules:
  - apiGroups: [""] # "" indicates the core API group
    resources: ["pods","endpoints","services","nodes"]
    verbs: ["get", "watch", "list"]
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "watch", "list", "update", "patch"]

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: manager
subjects:
  - kind: User
    name: system:serviceaccount:voip:default
    apiGroup: rbac.authorization.k8s.io
    # namespace: tools #specified namespace
roleRef:
  kind: Role
  name: voip-manager
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: Service
metadata:
  name: nats
  labels:
    component: nats
spec:
  selector:
    component: nats
  ports:
  - name: client
    port: 4222
  - name: cluster
    port: 6222
  - name: monitor
    port: 8222

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nats
  labels:
    component: nats
spec:
  replicas: 1
  selector:
    matchLabels:
      component: nats
  template:
    metadata:
      labels:
        component: nats
    spec:
      containers:
      - name: gnatsd
        image: nats
        ports:
        - containerPort: 4222 
          name: client
        - containerPort: 6222 
          name: cluster
        - containerPort: 8222 
          name: monitor
        livenessProbe:
          tcpSocket:
            port: 8222
          initialDelaySeconds: 15
          timeoutSeconds: 5
        readinessProbe:
          tcpSocket:
            port: 8222
          initialDelaySeconds: 15
          timeoutSeconds: 5
        resources:
          requests:
            cpu: "1"
            memory: "799M"
          limits:
            cpu: "2"
            memory: "1000M"

---
apiVersion: v1
kind: Service
metadata:
  name: kamailio
  labels:
    component: kamailio
spec:
  ports:
    - name: sip
      protocol: UDP
      port: 5060
      targetPort: 5060
  selector:
    component: kamailio

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kamailio
  labels:
    component: kamailio
spec:
  selector:
    matchLabels:
      component: kamailio
  template:
    metadata:
      name: kamailio
      labels:
        component: kamailio
    spec:
      nodeSelector:
        cloud.google.com/gke-nodepool: kamailio
      hostNetwork: true
      volumes:
        - name: config
      containers:
        - name: kamailio
          image: cycoresystems/kamailio
          resources:
            requests:
              cpu: "1"
              memory: "799M"
            limits:
              cpu: "1"
              memory: "1000M"
          env:
            - name: CLOUD
              value: gcp
          volumeMounts:
            - name: config
              mountPath: /data/kamailio
        - name: rtpproxy
          image: cycoresystems/rtpproxy
          resources:
            requests:
              cpu: "1"
              memory: "799M"
            limits:
              cpu: "1"
              memory: "1000M"
          env:
            - name: CLOUD
              value: gcp
        - name: dispatchers
          image: cycoresystems/dispatchers
          resources:
            requests:
              cpu: "1"
              memory: "799M"
            limits:
              cpu: "1"
              memory: "1000M"
          command:
            - /app
            - "-set"
            - voip:asterisk=1:5080
          volumeMounts:
            - name: config
              mountPath: /data/kamailio

---
apiVersion: v1
kind: Service
metadata:
  name: asterisk
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  selector:
    component: asterisk
  ports:
    - name: sip
      protocol: UDP
      port: 5060

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: asterisk
  labels:
    component: asterisk
spec:
  replicas: 1
  selector:
    matchLabels:
      component: asterisk
  template:
    metadata:
      labels:
        component: asterisk
    spec:
      volumes:
        - name: config
          emptyDir: {}
        - name: custom
          secret:
            secretName: asterisk-config
      containers:
        - name: asterisk
          image: cycoresystems/asterisk:16.6.1
          resources:
            requests:
              cpu: "1"
              memory: "799M"
            limits:
              cpu: "1"
              memory: "1000M"
          volumeMounts:
            - name: config
              mountPath: /etc/asterisk
        - name: config
          image: cycoresystems/asterisk-config:v0.9.1
          resources:
            requests:
              cpu: "1"
              memory: "799M"
            limits:
              cpu: "1"
              memory: "1000M"
          volumeMounts:
            - name: config
              mountPath: /etc/asterisk
            - name: custom
              mountPath: /source
          env:
            - name: CLOUD
              value: gcp
            - name: ARI_APPLICATION
              value: demo
            - name: ARI_USERNAME
              value: admin
            - name: ARI_PASSWORD
              value: admin
        - name: natsgw
          image: cycoresystems/ari-proxy:v5.0.1
          resources:
            requests:
              cpu: "1"
              memory: "799M"
            limits:
              cpu: "1"
              memory: "1000M"
          env:
            - name: NATS_URI
              value: nats://nats:4222
            - name: ARI_APPLICATION
              value: demo
            - name: ARI_USERNAME
              value: admin
            - name: ARI_PASSWORD
              value: admin